﻿using Bouyei.Geo.Geometries;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bouyei.Geo.GeoParsers.Wkt
{
    public class GeometryWriter
    {
        public GeometryWriter()
        {

        }

        public string Writer(Geometry geometry)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(geometry.GeometryType.ToString());

            if (geometry.GeometryType == GeoType.POINT)
            {
                Coordinate point = geometry.GetSequence(0, 0)[0];
                builder.Append("(");
                builder.Append(point.To2DString(" "));
                builder.Append(")");
            }
            else if (geometry.GeometryType == GeoType.LINESTRING
                || geometry.GeometryType == GeoType.LINESEGMENT)
            {
                var seqs = geometry.GetSequence(0, 0);
                builder.Append("(");
                builder.Append(string.Join(",", seqs.GetCoordinates().Select(x => x.To2DString(" "))));
                builder.Append(")");
            }
            else if (geometry.GeometryType == GeoType.POLYGON)
            {
                var seqs = geometry.GetGemoetry(0);
                builder.Append("(");

                for (int i = 0; i < seqs.Count; ++i)
                {
                    builder.Append("(");
                    builder.Append(string.Join(",", seqs[i].GetCoordinates().Select(x => x.To2DString(" "))));
                    builder.Append(")");
                    if (i < seqs.Count - 1) builder.Append(",");
                }
                builder.Append(")");
            }
            else if (geometry.GeometryType == GeoType.MULTIPOINT)
            {
                var seqs = geometry.GetSequence(0, 0);
                builder.Append("(");
                builder.Append(string.Join(",", seqs.GetCoordinates().Select(x => "(" + x.To2DString(" ") + ")")));
                builder.Append(")");
            }
            else if (geometry.GeometryType == GeoType.MULTILINESTRING)
            {
                var seqs = geometry.GetGemoetry(0);
                builder.Append("(");

                for (int i = 0; i < seqs.Count; ++i)
                {
                    builder.Append("(");
                    builder.Append(string.Join(",", seqs[i].GetCoordinates().Select(x => x.To2DString(" "))));
                    builder.Append(")");
                    if (i < seqs.Count - 1) builder.Append(",");
                }
                builder.Append(")");
            }
            else if (geometry.GeometryType == GeoType.MULTIPOLYGON)
            {
                builder.Append("(");
                for (int j = 0; j < geometry.GeometryCount; ++j)
                {
                    var seqs = geometry.GetGemoetry(j);
                    builder.Append("(");

                    for (int i = 0; i < seqs.Count; ++i)
                    {
                        builder.Append("(");
                        builder.Append(string.Join(",", seqs[i].GetCoordinates().Select(x => x.To2DString(" "))));
                        builder.Append(")");
                        if (i < seqs.Count - 1) builder.Append(",");
                    }

                    builder.Append(")");
                }
                builder.Append(")");
            }
            else throw new Exception("gemoetryType not supported" + geometry.GeometryType);

            return builder.ToString();
        }
    }
}
